﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WinTime.Data.Entity;

namespace WinTime.Data;
public class ApplicationDbContext : DbContext
{
    private readonly string _connectionString;

    public ApplicationDbContext(string connectionString)
    {
        _connectionString = connectionString;
    }

    public ApplicationDbContext()
    {
        _connectionString = DbConnConst.SqliteConn;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        optionsBuilder.UseSqlite(_connectionString);
    }

    public DbSet<AppModel> AppModels
    {
        get; set;
    }
    public DbSet<CategoryModel> CategoryModels
    {
        get; set;
    }

    public DbSet<ActivityRecord> ActivityRecords;

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<AppModel>(entity =>
        {
            entity.Property(x => x.ID).ValueGeneratedOnAdd();

            entity.HasOne(x => x.Category)
            .WithMany()
            .HasForeignKey(x => x.CategoryID);
        });

        modelBuilder.Entity<CategoryModel>(entity =>
        {
            entity.Property(x => x.ID).ValueGeneratedOnAdd();
        });

        modelBuilder.Entity<ActivityRecord>(entity =>
        {
            entity.Property(x => x.ID).ValueGeneratedOnAdd();

            entity.HasOne(x => x.AppModel)
            .WithMany()
            .HasForeignKey(x => x.AppModelID);
        });

    }


    public void InitDataSeed()
    {
        Add(new CategoryModel { ID = 1, Name = "未分类", Color = ColorTranslator.ToHtml(Color.Gray), IconFile = "" });
        Add(new CategoryModel { ID = 2, Name = "工作", Color = ColorTranslator.ToHtml(Color.OrangeRed), IconFile = "" });
        Add(new CategoryModel { ID = 3, Name = "冲浪", Color = ColorTranslator.ToHtml(Color.ForestGreen), IconFile = "" });
        Add(new CategoryModel { ID = 4, Name = "影音", Color = ColorTranslator.ToHtml(Color.Yellow), IconFile = "" });
        Add(new CategoryModel { ID = 5, Name = "游戏", Color = ColorTranslator.ToHtml(Color.BlueViolet), IconFile = "" });
    }
}
